MacroScript ImportMesh category:"Fili" tooltip:"Imports binary data from a 3DB file" icon:#("Maxscript",2)
(
	persistent global lastPathOpen
	if lastPathOpen == undefined then 
	(
		lastPathOpen = ""
	)

	in_name = GetOpenFileName \
		caption: "Select path for imported object" \
		types: "3D BinObjects (*.3DB)|*.3db|All Files (*.*)|*.*" \
		filename:lastPathOpen

	if in_name != undefined then
	(
		lastPathOpen = in_name
		--open file in binary read mode
		in_binStream = fopen in_name "rb"

		--read identifier 3DBO
		file_ident = readLong in_binStream
		
		if file_ident != 0x4F424433 then
		(
			MessageBox "This file doesn't seem to be a valid object"
			return 0
		)
		
		
		--read file version
		file_vers = readByte in_binStream
		
		if file_vers != 0x01 then
		(
			MessageBox "Unsuported version"
			return 0
		)
		
		--read numbers :P
		num_verts = readLong in_binStream
		num_tverts = readLong in_binStream
		num_faces = readLong in_binStream
		
		--write padding bytes up to header size (32 bytes)
		for i = 18 to 32 do
			readByte in_binStream tmp
			
		vert_array = #()
		tvert_array = #()
		facev_array = #()
		facetv_array = #()
		facen_array = #()
		faces_array = #()
		facef_array = #()
			
		--read vertex coordinates
		for i = 1 to num_verts do
		(
			x = readFloat in_binStream
			y = readFloat in_binStream
			z = readFloat in_binStream		
			append vert_array [x,y,z]
		)
			
		--read texture coordinates
		for i = 1 to num_tverts do
		(		
			x = readFloat in_binStream
			y = readFloat in_binStream			
			append tvert_array [x,y,0]
		)
		
		--read face data
		for i = 1 to num_faces do
		(		
			p1 = (readLong in_binStream) + 1
			p2 = (readLong in_binStream) + 1
			p3 = (readLong in_binStream) + 1			
			append facev_array [p1,p2,p3]

			p1 = (readLong in_binStream) + 1
			p2 = (readLong in_binStream) + 1
			p3 = (readLong in_binStream) + 1			
			append facetv_array [p1,p2,p3]
			
			
			tmp_normal = #()
			--normals
			for j = 1 to 3 do
			(
				x = readFloat in_binStream
				y = readFloat in_binStream
				z = readFloat in_binStream
				
				append tmp_normal [x,y,z]
			)
			
			append facen_array tmp_normal
			
			sgroup = readLong in_binStream
			append faces_array sgroup
			
			flags = readLong in_binStream
			append facef_array flags
		)
		
		
		new_mesh = mesh vertices:vert_array faces:facev_array
		
		setNumTVerts new_mesh num_tverts
		
		for i=1 to num_tverts do
			setTVert new_mesh i  tvert_array[i]
		
		buildTVFaces new_mesh
		
		for i=1 to num_faces do
		(
			setTVFace new_mesh i facetv_array[i]
			setFaceSmoothGroup new_mesh i faces_array[i]
						
			if (bit.and facef_array[i] 0x01)==0 then
				setEdgeVis new_mesh i 1 false
			if (bit.and facef_array[i] 0x02)==0 then
				setEdgeVis new_mesh i 2 false
			if (bit.and facef_array[i] 0x04)==0 then
				setEdgeVis new_mesh i 3 false
		)
		
		update new_mesh
		--everything ok, close the file
		FClose in_binStream
		
		messageBox ("Mesh data imported successfully from:\n"+in_name) title:"Success"
	)
)